<template>
	<div class="content-wrap">
		<div class="content-top-box">
			<!-- 搜索区域 -->
			<ul class="search-container">
				<li>
					<span class="search-item-name">手机号：</span>
					<el-input class="search-item-content search-item-text" placeholder="请输入手机号" v-model="phone" clearable maxlength="11" @clear="clearPhoneFn">
					</el-input>
				</li>
				<li>
					<span class="search-item-name">姓名：</span>
					<el-input class="search-item-content search-item-text" placeholder="请输入姓名" v-model="userName" clearable @clear="clearUserName">
					</el-input>
				</li>
				<li>
					<span class="search-item-name">采购时间：</span>
					<el-date-picker v-model="date" type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss" @change="changeDate">
					</el-date-picker>
				</li>
				<li>
					<span class="search-item-name">采购状态：</span>
					<el-select class="search-item-content" v-model="state" placeholder="请选择" clearable @change="changePurchaseType">
						<el-option v-for="item in purchaseTypeOptions" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</li>
				<li>
					<el-button class="search-btn" type="primary" icon="el-icon-search" @click="searchFn" v-if="searchBtn">查询</el-button>
					<download-excel class="export-excel-wrapper" :data="jsonData" :fields="jsonFields" name="黑木耳订单表.xls" v-if="exportBtn">
						<!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
						<el-button class="search-btn" type="primary" icon="el-icon-download">导出</el-button>
					</download-excel>
				</li>
			</ul>
		</div>

		<div class="content-bottom-box">
			<!-- 表格 -->
			<div class="content-form-box">
				
				<el-table v-loading="loadingFlag" :data="tableData" :height="tableHeight" ref="table" border stripe style="width: 100%">
					<el-table-column prop="serialNumbers" label="序号" width="60" :show-overflow-tooltip="true" fixed>
					</el-table-column>
					<el-table-column prop="createTime" label="采购时间" width="160" :show-overflow-tooltip="true" fixed>
					</el-table-column>
					<el-table-column prop="state" label="采购状态" width="100" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="userName" label="姓名" :show-overflow-tooltip="true" width="80">
					</el-table-column>
					<el-table-column prop="phone" label="手机号" width="120" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="productLevel" label="木耳等级" width="100" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="productWeight" label="木耳重量" width="100">
						<template slot-scope="scope">
							<span>
			                    {{scope.row.productWeight}}{{scope.row.productWeightUnit}}
			                </span>
						</template>
					</el-table-column>
					<el-table-column prop="productSpecs" label="木耳规格" width="180" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="productSalePrice" label="木耳单价" :show-overflow-tooltip="true" width="120">
						<template slot-scope="scope">
							<span>
			                    {{scope.row.productSalePrice}}{{scope.row.productSalePriceUnit}}
			                </span>
						</template>
					</el-table-column>
					<el-table-column prop="orderQuantity" label="采购数量" width="120" :show-overflow-tooltip="true">
						<template slot-scope="scope">
							<span>
								{{scope.row.orderQuantity}}{{scope.row.orderQuantityUnit}}
			                </span>
						</template>
					</el-table-column>
					<el-table-column prop="orderMoney" label="采购金额(元)" width="140" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="zfMoney" label="支付金额(元)" width="120" :show-overflow-tooltip="true">
					</el-table-column> 
					<el-table-column prop="payMethod" label="支付方式" width="120" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="payTime" label="支付时间" width="160" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="address" label="收件地址" width="220" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="expressFee" label="物流费用" width="100">
					</el-table-column>
					<el-table-column prop="outTradeNo" label="订单编号" width="180" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="contractCode" label="合同编号" width="160" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column label="操作" fixed="right">
						<template slot-scope="scope">
							<el-button @click="checkUserInfoFn(scope.row)" type="text" size="small" v-if="detailBtn">详情</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>

			<!-- 页码 -->
			<div class="content-Page-container">
				<BaseFormPage :pagination="pagination" @cFirstPageFn="cFirstPageFn" @cLastPageFn="cLastPageFn" @cJumpPageFn="cJumpPageFn" @cPreFn="cPreFn" @cNextFn="cNextFn" @cRefreshFn="cRefreshFn">
				</BaseFormPage>
			</div>
		</div>

		<!-- 弹窗 -->
		<el-dialog title="订单详情" :visible.sync="dialogFlag" :show-close="false" width="804px">
			<div class="dialog-box">
				<div class="info-detail-box">
					<div class="info-detail-list">
						<ul>
							<li class="list-title">用户信息</li>
							<li>
								<span class="info-tag-name">姓名：</span>
								<span class="info-tag-text">{{detailData.userName}}</span>
							</li>
							<li>
								<span class="info-tag-name">手机号：</span>
								<span class="info-tag-text">{{detailData.phone}}</span>
							</li>
							<li>
								<span class="info-tag-name">性别：</span>
								<span class="info-tag-text">{{detailData.gender}}</span>
							</li>
						</ul>
						<ul>
							<li class="list-title">订单信息</li>
							<li>
								<span class="info-tag-name">订单编号：</span>
								<span class="info-tag-text">{{detailData.outTradeNo}}</span>
							</li>
							<li>
								<span class="info-tag-name">支付时间：</span>
								<span class="info-tag-text">{{detailData.payTime}}</span>
							</li>
							<li>
								<span class="info-tag-name">采购状态：</span>
								<span class="info-tag-text">{{detailData.state}}</span>
							</li>
							<li>
								<span class="info-tag-name">木耳等级：</span>
								<span class="info-tag-text">{{detailData.productLevel}}</span>
							</li>
							<li>
								<span class="info-tag-name">木耳规格：</span>
								<span class="info-tag-text">{{detailData.productSpecs}}</span>
							</li>
							<li>
								<span class="info-tag-name">木耳单价：</span>
								<span class="info-tag-text">{{detailData.productSalePrice}}{{detailData.productSalePriceUnit}}</span>
							</li>
							<li>
								<span class="info-tag-name">采购数量：</span>
								<span class="info-tag-text">{{detailData.orderQuantity}}{{detailData.orderQuantityUnit}}</span>
							</li>
						</ul>
					</div>

					<ul class="info-detail-list">
						<li class="list-title"></li>
						<li>
							<span class="info-tag-name">木耳重量：</span>
							<span class="info-tag-text">{{detailData.productWeight}}{{detailData.productWeightUnit}}</span>
						</li>
						<li>
							<span class="info-tag-name">采购木耳重量：</span>
							<span class="info-tag-text">{{detailData.orderTotalWeight}}{{detailData.orderTotalWeightUnit}}</span>
						</li>
						<li>
							<span class="info-tag-name">包装方式：</span>
							<span class="info-tag-text">{{detailData.productPacking}}</span>
						</li>
						<li>
							<span class="info-tag-name">最低起购：</span>
							<span class="info-tag-text">{{detailData.minOrderQuantity}}{{detailData.minOrderQuantityUnit}}</span>
						</li>
						<li>
							<span class="info-tag-name">产地：</span>
							<span class="info-tag-text">{{detailData.productOriginProvince}}{{detailData.productOriginCity}}</span>
						</li>
						<li>
							<span class="info-tag-name">订单金额(元)：</span>
							<span class="info-tag-text">{{detailData.orderMoney}}</span>
						</li> 
						<li>
							<span class="info-tag-name">支付方式：</span>
							<span class="info-tag-text">{{detailData.payMethod}}</span>
						</li>
						<li>
							<span class="info-tag-name">物流状态：</span>
							<span class="info-tag-text">{{detailData.state}}</span>
						</li>
						<li>
							<span class="info-tag-name">物流费用：</span>
							<span class="info-tag-text">{{detailData.expressFee}}</span>
						</li>
						<li>
							<span class="info-tag-name">合同编号：</span>
							<span class="info-tag-text">{{detailData.contractCode}}</span>
						</li>
						<li class="last-oli">
							<span class="info-tag-name last-ospan">收件地址：</span>
							<span class="info-tag-text last-span2">{{detailData.addressProvince}}{{detailData.addressCity}}{{detailData.addressCounty}}{{detailData.addressDetail}}</span>
						</li>
					</ul>
				</div>
				<div class="dialog-btn">
					<el-button type="primary" size="medium" @click="dialogFlag = false">关闭</el-button>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import Storage from "@/utils/storage";
	import BaseFormPage from '@/components/BaseFormPage';
	export default {
		components: {
			BaseFormPage
		},
		inject: ['reload'],
		data() {
			return {
				loadingFlag: false, //表格数据加载状态
				dialogFlag: false, //弹窗显示状态
				phone: '',
				userName: '',
				state: '',
				date: '',
				startCreateTime: '',
				endCreateTime: '',
				purchaseTypeOptions: [{
						value: '0',
						label: '待付款'
					},
					{
						value: '1',
						label: '待发货'
					},
					{
						value: '2',
						label: '待收货'
					},
					{
						value: '3',
						label: '已完成'
					}
				],

				pageIndex: 1,
				pageSize: 20,
				tableHeight: 200, //表格初始高度
				tableData: [], //表格数据
				pagination: { //分页数据
					loadingFlag: false, //数据加载状态
					currentPage: 1, //当前页
					totalPage: 1, //总页数
					totalNum: 0, //总条数
					firstNum: 0, //分页显示起始值
					endNum: 0 //分页显示结束值
				},
				jsonData: [],
				jsonFields: {
					"序号": "index",
					"采购时间": "createTime",
					"采购状态": "state",
					"姓名": "userName",
					"手机号": "phone",
					"木耳等级": "productLevel",
					"木耳重量": "productWeight",
					"木耳规格": "productSpecs",
					"木耳单价": "productSalePrice",
					"采购数量": "orderQuantity",
					"采购金额(元)": "orderMoney",
					"支付金额(元)": "zfMoney",
					"支付方式": "payMethod",
					"支付时间": "payTime",
					"收件地址": "address",
					"物流费用": "expressFee",
					"订单编号": "outTradeNo",
					"合同编号": "contractCode",
				},
				jsonMeta: [
					[{
						"key": "charset",
						"value": "utf-8"
					}]
				],
				detailData: {
					
				},
				detailId: '',

				searchBtn: (JSON.parse(Storage.get('roleAuthIdArr'))).indexOf(225) == -1 ? false : true,
				exportBtn: (JSON.parse(Storage.get('roleAuthIdArr'))).indexOf(226) == -1 ? false : true,
				detailBtn: (JSON.parse(Storage.get('roleAuthIdArr'))).indexOf(227) == -1 ? false : true,
			}
		},
		created() {
			this.getListData()
		},
		methods: {
			getListData(){    // 审核列表
				this.loadingFlag = true;
				this.pagination.loadingFlag = true;
				let params = {
					phone: this.phone,
					userName: this.userName,
					state: this.state,
					startCreateTime: this.startCreateTime,
					endCreateTime: this.endCreateTime,
					pgCt: this.pageIndex,
					pgSz: this.pageSize
				}
				if(!this.phone) {
					delete params.phone
				}
				if(!this.userName) {
					delete params.userName
				}
				if(!this.state) {
					delete params.state
				}
				if(!this.startCreateTime) {
					delete params.startCreateTime
				}
				if(!this.endCreateTime) {
					delete params.endCreateTime
				}
				this.$http.get('/fungusPurchase/listFungusPurchase', params)
				.then((res)=>{
					console.log(res.data.records)
					if(res.data.records.length > 0) {
							this.pagination.totalPage = res.data.pages;
							this.pagination.totalNum = res.data.total;
							res.data.records.forEach((item, index) => {
								if(item.payMethod == 'overpay'){
									item.payMethod = '余额支付'
								}else if(item.payMethod == 'wechat'){
									item.payMethod = '微信支付'
								}
								if(item.state == 0){
									item.state = '待付款'
								} else if(item.state == 1){
									item.state = '待发货'
								}else if(item.state == 2){
									item.state = '待收货'
								}else if(item.state == 3){
									item.state = '已完成'
								}else if(item.state == 99){
									item.state = '已失效'
								}
								item.zfMoney = item.orderMoney + item.expressFee;  // 支付金额 = 采购金额 + 物流费用
								item.address = item.addressProvince + item.addressCity + item.addressCounty + item.addressDetail 
								//列表下标
								item.index = index
								//列表序号
								item.serialNumbers = ((this.pageIndex - 1) * this.pageSize) + (index + 1)
								//无数据处理
//								item.reviewRemark = item.reviewRemark || '--'
							})
							this.pagination.firstNum = res.data.records[0].serialNumbers
							this.pagination.endNum = res.data.records[res.data.records.length - 1].serialNumbers
						} else {
							this.pagination.currentPage = 1
							this.pagination.totalPage = 1
							this.pagination.totalNum = 0
							this.pagination.firstNum = 0
							this.pagination.endNum = 0
						}
						this.tableData = res.data.records;
				})
				.then(() => {
						if(this.tableData.length !== 0) {
							this.tableHeightFn();
						}
						this.loadingFlag = false;
						this.pagination.loadingFlag = false;
						this.getAllListData()
					})
			},
			getDetailData(){ // 详情
				this.$http.get('/fungusPurchase/getFungusPurchase' + this.detailId)
				.then((res)=>{
					this.detailData = res.data;
					console.log(res)
				})
			},
			tableHeightFn() { //表格高度
				let tableDataLength = this.tableData.length;
				let minHeight = tableDataLength == 0 ? 0 : 80;
				this.$nextTick(function() {
					this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - minHeight;
					if(this.tableData.length <= 0) {
						this.tableHeight = '60vh'
					}

					let self = this;
					window.onresize = function() {
						self.tableHeight = window.innerHeight - self.$refs.table.$el.offsetTop - minHeight;
						self.tableHeight = self.tableHeight < 200 ? 200 : self.tableHeight;
					}
				})
			},
			resetPageIndex() { //重置页码
				this.pagination.currentPage = 1;
				this.pageIndex = 1;
			},
			clearPhoneFn() { //清空手机号
				this.resetPageIndex()
				this.getListData()
			},
			clearUserName() { //清空姓名
				this.resetPageIndex()
				this.getListData()
			},
			changeDate() { //选择时间
				if(this.date) {
					this.startCreateTime = this.date[0]
					this.endCreateTime = this.date[1].replace('00:00:00', '23:59:59')
				} else {
					this.startCreateTime = ''
					this.endCreateTime = ''
				}
				this.resetPageIndex()
				this.getListData()
			},
			changePurchaseType() { //选择状态
				this.resetPageIndex()
				this.getListData()
			},
			searchFn() { //搜索
				this.resetPageIndex()
				this.getListData()
			},
			checkUserInfoFn(row) { //查看详情
				this.dialogFlag = true;
				this.detailData = row;
				this.detailId = row.id;
			},
			cFirstPageFn(pageNum) { //首页
				this.pageIndex = pageNum;
				this.getListData()
			},
			cLastPageFn(pageNum) { //尾页
				this.pageIndex = pageNum;
				this.getListData()
			},
			cJumpPageFn(pageNum) { //页码跳转
				this.pageIndex = pageNum;
				this.getListData()
			},
			cPreFn(pageNum) { //上一页
				this.pageIndex = pageNum;
				this.getListData()
			},
			cNextFn(pageNum) { //下一页
				this.pageIndex = pageNum;
				this.getListData()
			},
			resetParams() { //重置参数
				this.reload()
			},
			cRefreshFn() { //刷新
				this.resetParams()
				this.getListData()
			},
			getAllListData(){    // 审核列表
				let params = {
					phone: this.phone,
					userName: this.userName,
					state: this.state,
					startCreateTime: this.startCreateTime,
					endCreateTime: this.endCreateTime,
					pgCt: 1,
					pgSz: 99999
				}
				if(!this.phone) {
					delete params.phone
				}
				if(!this.userName) {
					delete params.userName
				}
				if(!this.state) {
					delete params.state
				}
				if(!this.startCreateTime) {
					delete params.startCreateTime
				}
				if(!this.endCreateTime) {
					delete params.endCreateTime
				}
				this.$http.get('/fungusPurchase/listFungusPurchase', params)
				.then((res)=>{
					console.log(res)
					if(res.data.records.length > 0) {
							res.data.records.forEach((item, index) => {
								if(item.payMethod == 'overpay'){
									item.payMethod = '余额支付'
								}else if(item.payMethod == 'wechat'){
									item.payMethod = '微信支付'
								}
								if(item.state == 0){
									item.state = '待付款'
								} else if(item.state == 1){
									item.state = '待发货'
								}else if(item.state == 2){
									item.state = '待收货'
								}else if(item.state == 3){
									item.state = '已完成'
								}else if(item.state == 99){
									item.state = '已失效'
								}
								if(item.productWeight){
									item.productWeight = item.productWeight + item.productWeightUnit
								}
								if(item.productSalePrice){
									item.productSalePrice = item.productSalePrice + item.productSalePriceUnit
								}
								if(item.orderQuantity){
									item.orderQuantity = item.orderQuantity + item.orderQuantityUnit
								}
								item.zfMoney = item.orderMoney + item.expressFee;  // 支付金额 = 采购金额 + 物流费用
								item.address = item.addressProvince + item.addressCity + item.addressCounty + item.addressDetail 
								//列表下标
								item.index = index + 1
								//列表序号

							})
						} 
						this.jsonData = res.data.records;
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.dialog-box {
		.info-detail-box {
			width: 100%;
			display: flex;
			.info-detail-list {
				width: 50%;
				&:nth-of-type(1) {
					padding-right: 5px;
					box-sizing: border-box;
				}
				&:nth-of-type(2) {
					padding-left: 5px;
					box-sizing: border-box;
				}
				li {
					width: 100%;
					display: flex;
					align-items: center;
					margin-bottom: 12px;
					.info-tag-name {
						width: 144px;
						text-align: right;
					}
					.info-tag-text {}
				}
				.list-title {
					padding-left: 50px;
					box-sizing: border-box;
					color: #344047;
					font-weight: 600;
				}
			}
		}
		.dialog-btn {
			width: 100%;
			padding-top: 28px;
			text-align: center;
		}
	}
	.last-oli{
		display: inherit; 
		padding-left: 74px;
		position: relative;
		.last-ospan{
			position: absolute;
			left: 0;
			top: 0;
		}
		.last-span2{
			position: absolute;
			left: 145px;
			top: 0;
		}
	}
</style>